The present invention relates generally to compilation of computer programs and, more particularly, to the optimization of computer programs.
Computer programs can be very complex and consume enormous amounts of computational resources when executed. For example, a computer program that models the weather over the entire earth may model the weather to an accuracy of a square mile of the surface of the earth and at elevations that are multiples of 1000 feet. Thus, a complex computation may need to be performed for each square mile of the surface of the earth at various elevations. In addition, the complex computation may need to be repeated multiple times to simulate the changing weather conditions. Such complex computer programs, in practice, can only be executed on the fastest supercomputers. Even when executing such complex computer programs on a supercomputer, they may take too long to execute to be practical. Moreover, even less complex computer programs may consume enough computational resources so that a user of the computer program becomes frustrated with the speed of execution. For example, a user may replace a spreadsheet program that is slow at performing a recalculation with another spreadsheet program that can perform the recalculation much faster.
Computer programmers devote considerable effort to improving the efficiency of the computer programs that they develop. However, because of the complexities of the computer programs, programmers may not have the time to fully understand all the improvements that can be made. As a result, compilers often include an extensive optimization phase in which the compiler attempts to optimize (i.e., improved efficiency of) the computer program that is being compiled. Also, some optimizers have been developed to analyze and optimize executable code. The goal of these optimizers is to generate code that is as efficient as could possibly be generated by a programmer. Although no optimizer has achieved that goal, in practice, optimizers can significantly improve efficiency of a computer program. For example, if an optimizer can identify a statement whose execution can be removed from inside a loop that is executed for each square mile of the surface of the earth, then the computational efficiency of the program is improved. Although such an optimization may result in a relatively small increase in the overall speed of execution of the computer program, the effect of many such optimizations can be significant. Various optimization techniques are described xe2x80x9cCompilers: Principles, Techniques, and Tools,xe2x80x9d Aho, Sethi, and Ullman, Addison-Wesley, 1988, which is hereby incorporated by reference.
Embodiments of the present invention provide a method system for optimizing a computer program. In one embodiment, the system identifies depths of blocks of a computer program and identifies the availability of expressions of the computer program. The system then modifies the computer program when he identified availability of the expression and the identified depth of a block indicate that the expression can be moved to the block. The depth of the block may represent the number of dominator blocks of that block. The availability of the expression may represent the depth of a block to which the expression may be moved. In one embodiment, when the identified availability of the expression is less than the identified depth of the block, the expression can be moved to the block.
Another aspect of the present invention determines the availability of expressions of a computer program. The system for determining such availability visits blocks of the computer program during a forward traversal of a control flow graph representing that computer program. For each expression of each block visited, the system sets the availability of the expression based on the reaching definition when the operation of the expression is a load from memory. The system also sets the availability of an expression to the latest availability of its operands when the operation is not store from memory. The setting of the availability of the expression based on the reaching definition may include the setting of the availability of the expression to the availability of the reaching definition when the reaching definition is a store of the result of expression with the same value as the value of the expression.
Another aspect of the present invention identifies a direct dominator of a block of the computer program. To identify the direct dominator, the system identifies the closest dominator of the block such that the block is contained in the inner most region containing that dominator. That identified closest dominator is the direct dominator of the block. In one embodiment, the system identifies the direct dominator by first selecting the closest dominator of the block. The system then selects the least common region that contains the region that contains the block and the region that contains the selected dominator of the block. The system then loops searching for the least common region that is the same as an inner most region of a currently selected dominator.